package com.example.demo.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.example.demo.config.WxConfig;
import com.example.demo.service.WxService;
import com.example.demo.util.AjaxResult;
import com.example.demo.util.HttpClientUtis;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.Map;

/**
 * @ClassName:WxServiceImpl
 * @Description: TODO
 */
@Service
@Transactional
@Slf4j
public class WxServiceImpl implements WxService {

    @Override
    public AjaxResult wxLogin(String code, String encryptedData, String iv) {
        //封装请求参数
        Map<String,String> params = new HashMap<>();
        params.put("appid", WxConfig.APPID);
        params.put("secret",WxConfig.SECRET);
        params.put("js_code",code);
        params.put("grant_type",WxConfig.GRANT_TYPE);
        //请求微信登录授权
        String jsonString = HttpClientUtis.doGet(params,WxConfig.WXLOGIN_URL);
//        log
//        log.info("--------------->>>" + jsonString);
        JSONObject jsonObject = JSONObject.parseObject(jsonString);
        String openId = jsonObject.getString("openid");
        Map map = new HashMap();
        map.put("openId",openId);
        return new AjaxResult().ok(map);
    }
}
